﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;

namespace MyAspx.Framework.WebUnit.CodeZipTree
{
    public static class PathHelper
    {
        public static string Root = GetRootPath()+"/zipstore/";

        //public static string Root
        //{
        //    get
        //    {
        //        if (!Directory.Exists(root))
        //        {
        //            Directory.CreateDirectory(root);
        //        }
        //        return root;
        //    }
        //    set { root = value; }
        //}
        public static string NewRoot = "//";

        public static string ToNewPath(this string str)
        {
            var dir = str.Substring(Root.Length, str.Length- Root.Length);
            return string.Concat(NewRoot, dir).ToLower();
        }

        public static string ToOriginalPath(this string str)
        {
            var dir = str.Substring(NewRoot.Length, str.Length - NewRoot.Length);

            var ext= Path.GetExtension(dir);

            if (string.IsNullOrEmpty(ext)&& dir.Length>=1)
            {
                if (!dir.EndsWith("/"))
                {
                    dir = string.Concat(dir, "/");
                }
            }

            return Path.Combine(Root, dir);
        }

        public static bool IsRoot(this string str)
        {
            if (str.Length == Root.Length && str.ToLower() == Root.ToLower())
                return true;
            else
                return false;
        }
        public static bool IsNewRoot(this string str)
        {
            if (str.Length == NewRoot.Length && str.ToLower() == NewRoot.ToLower())
                return true;
            else
                return false;
        }
         /// 取得网站的根目录的URL
        /// </summary>
        /// <returns></returns>
        public static string GetRootURI()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            HttpRequest Req;
            if (HttpCurrent != null)
            {
                Req = HttpCurrent.Request;

                string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                    //直接安装在   Web   站点   
                    AppPath = UrlAuthority;
                else
                    //安装在虚拟子目录下   
                    AppPath = UrlAuthority + Req.ApplicationPath;
            }
            return AppPath;
        }
        /// <summary>
        /// 取得网站的根目录的URL
        /// </summary>
        /// <param name="Req"></param>
        /// <returns></returns>
        public static string GetRootURI(HttpRequest Req)
        {
            string AppPath = "";
            if(Req != null)
            {
                string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
                if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                    //直接安装在   Web   站点   
                    AppPath = UrlAuthority;
                else
                    //安装在虚拟子目录下   
                    AppPath = UrlAuthority + Req.ApplicationPath;
            }
            return AppPath;
        }
        /// <summary>
        /// 取得网站根目录的物理路径
        /// </summary>
        /// <returns></returns>
        public static string GetRootPath()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            if (HttpCurrent != null)
            {
                AppPath = HttpCurrent.Server.MapPath("~");
            }
            else
            {
                AppPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                    AppPath = AppPath.Substring(0, AppPath.Length - 1);
            }
            return AppPath;
        }
    }
}
